package com.xuan.temper.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xuan.temper.controller.MQProducer;
import com.xuan.temper.dao.ExponentialTemperatureDao;
import com.xuan.temper.entity.Data;
import com.xuan.temper.entity.ExponentialInfo;
import com.xuan.temper.entity.ExponentialTemperature;
import com.xuan.temper.entity.ReturnData;
import com.xuan.temper.service.CodeTempService;
import com.xuan.temper.service.ExponentialInfoService;
import com.xuan.temper.service.ExponentialTemperatureService;
import com.xuan.temper.utils.sendPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @创建人： king xuan
 * @创建时间：2020/8/27
 * @描述：
 */
@Service
public class CodeTempImpl implements CodeTempService {


    @Autowired
    private ExponentialInfoService exponentialInfoService;

    @Autowired
    private ExponentialTemperatureDao exponentialTemperatureDao;

    @Autowired
    private MQProducer mqProducer;

    @Override
    public List<Data> codeTemp(String startDate, String endDate, String stockCodes, String[] current) {
        String url = "https://open.lixinger.com/api/a/index/fundamental";
        String stockCodes1[] = {stockCodes};
        //String metricsList[] = {"pe_ttm.mcw", "pb.mcw"};

        Map<String, Object> params = new HashMap<>();
        params.put("token", "8b5f363a-417a-4ac8-98ad-c2cfbd12a7fd");
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("stockCodes", stockCodes1);
        params.put("metricsList", current);
        String paramsJson = JSON.toJSONString(params); // 将参数转换为json字符串
        //System.out.println(paramsJson);
        String vale = sendPost.sendPost(url, paramsJson);
        ReturnData jsonValue = JSON.parseObject(vale,ReturnData.class);
        //System.out.println(vale);
        List<Data> data = jsonValue.getData();
        //pe
        ArrayList<Double> peList = new ArrayList<>();
        //pb
        ArrayList<Double> pbList = new ArrayList<>();
        for (Data datum : data) {
            String stockCode = datum.getStockCode();
            Date date = datum.getDate();
            Double pe_ttm = datum.getPe_ttm().getMcw();
            Double pb = datum.getPb().getMcw();
            peList.add(pe_ttm);
            pbList.add(pb);
        }
        Collections.sort(peList);
        Collections.sort(pbList);
        //数组大小
        Double peListSize = Double.valueOf(peList.size());
        Integer pbListSize = pbList.size();
        //间隔因数
        Double i = 1 / peListSize;
        for(int j=0;j<pbListSize;j++){
            double pbValue = pbList.get(j);
            double peValue = peList.get(j);
            double pbTemper = (j * i)*100;
            double peTemper = (j * i)*100;
            for (Data data1 : data) {
                if (pbValue==data1.getPb().getMcw()){
                    data1.getPb().setPbTemper(pbTemper);
                }
                if (peValue==data1.getPe_ttm().getMcw()){
                    data1.getPe_ttm().setPeTemper(peTemper);
                }
            }
        }
        SimpleDateFormat sd=new SimpleDateFormat("yyyy年MM月dd日");
        long startTime=System.currentTimeMillis();   //获取开始时间
        ExponentialInfo exponentialInfo = exponentialInfoService.queryByStockCode(data.get(0).getStockCode());
        String name = exponentialInfo.getName();
        for (Data returnData : data) {
            System.out.println("当前指数代码为: " + returnData.getStockCode());

//            System.out.println("日期 = " + sd.format(returnData.getDate()));
//            System.out.println("pe值 = " + returnData.getPe_ttm().getMcw());
//            System.out.println("pe温度 = " + returnData.getPe_ttm().getPeTemper());
//            System.out.println("pb值 = " + returnData.getPb().getMcw());
//            System.out.println("pb温度 = " + returnData.getPb().getPbTemper());
            Double CurrenTemp= (returnData.getPe_ttm().getPeTemper()+returnData.getPb().getPbTemper())/2;
            returnData.setCurrenTemp(CurrenTemp);
            returnData.setStockName(name);
         //   System.out.println("指数温度 = " + CurrenTemp );
         //System.out.println("指数ROE = " +(returnData.getPbTemper()/returnData.getPeTemper()) *100+"%");
        //    System.out.println("------------------------------");

//            ExponentialTemperature temperature = new ExponentialTemperature();
//            temperature.setPeJq(returnData.getPe_ttm().getMcw());//pe值
//            temperature.setPeTemp(returnData.getPe_ttm().getPeTemper());//pe温度
//            temperature.setPbJq( returnData.getPb().getMcw());//pb值
//            temperature.setPbTemp(returnData.getPb().getPbTemper());//pb温度
//            temperature.setCurrenTemp(CurrenTemp);//指数温度
//            temperature.setStockCode(returnData.getStockCode());//指数代码
//            temperature.setName(name);//指数名称
//            temperature.setDyr(returnData.getDyr().getMcw());//股息率
//            temperature.setCp(returnData.getCp());//市值
//            temperature.setCpc(returnData.getCpc());//涨跌幅
//            temperature.setTime(returnData.getDate());
//            temperature.setUpdateTime(new Date());
//            SimpleDateFormat sg =new SimpleDateFormat("yyyy-MM-dd");
//
//            String formatDate = sg.format(returnData.getDate());
//            String stockCode = returnData.getStockCode();
//            mqProducer.sendMessage(formatDate+"+"+stockCode, "data", "dataDelete", "Key" + i);
//
//            String s = JSON.toJSONString(temperature);
//            mqProducer.sendMessage(s, "data", "dataInsert", "Key" + i);

            String db = JSON.toJSONString(returnData);
            mqProducer.sendMessage(db, "data", "dataInsert", "Key" + i);



            //Boolean delete = exponentialTemperatureDao.delete(sg.format(returnData.getDate()),returnData.getStockCode());
          //  Boolean insert = exponentialTemperatureDao.insert(temperature);

//            if (delete){
//                System.out.println("删除成功");
//            }else {
//                System.out.println("删除失败");
//            }
//            if (insert){
//                System.out.println("新增成功");
//            }else {
//                System.out.println("新增失败");
//            }
        }
        long endTime=System.currentTimeMillis(); //获取结束时间

        System.out.println("程序运行时间： "+(endTime-startTime)+"ms");
        return data;
    }
}
